# -*- coding: utf-8 -*-
# @Time    : 2021/4/16 14:59
# @Author  : lilu
# @Email   : 1906196327@qq.com
# @File    : 局域网IP存活检测.py
# @Software: PyCharm

import subprocess
import  IPy
ip = input("请输入要检查的IP网段、子网掩码：")
try:
    ips = IPy.IP(ip)
    print(len(ips))
    for i in ips:
        p = subprocess.getoutput(f"ping -n 1 -w 1 {i}")
        if "请求超时" in p:
            continue
        else:
            ip_useds = str(i)+"该IP已经被使用"
            with open("ips_used.txt", "a+", encoding="utf-8") as f:
                f.write(ip_useds+"\n")
                f.flush()
    print("判断结束！")
except ValueError as e:
    print("输入有误！")

